package org.yo.router.compiler;

import java.io.IOException;
import java.io.Writer;

/**
 * @author hailong
 * @date 2021年11月05日 4:34 下午
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 */
public class YoApplicationGenerator {

    private String appProxyPackage;
    private String appProxyName;

    public YoApplicationGenerator(String appProxyPackage, String appProxyName) {
        this.appProxyPackage = appProxyPackage;
        this.appProxyName = appProxyName;
    }

    public void generateYoApplication(Writer writer, String appPackage, String appName) throws IOException {
        writer.write("package " + appProxyPackage + ";\n\n");
        writer.write("import android.app.Application;\n\n");
        writer.write("import android.content.res.Configuration;\n\n");
        writer.write("import org.yo.router.annotation.framework.ApplicationAble;\n\n");

        writer.write("public class " + appProxyName + " implements ApplicationAble{ \n\n");

        writer.write("    private ApplicationAble proxy; \n\n");

        writer.write("    public " + appProxyName + "() { \n\n");
        writer.write("        this.proxy= new " + appPackage + "." + appName + "();\n\n");
        writer.write("    } \n\n");

        writer.write("    @Override \n");
        writer.write("    public void onCreate(Object context) { \n\n");
        writer.write("        proxy.onCreate((Application)context); \n\n");
        writer.write("    } \n\n");

        writer.write("    @Override \n");
        writer.write("    public void onLowMemory() { \n\n");
        writer.write("        proxy.onLowMemory(); \n\n");
        writer.write("    } \n\n");

        writer.write("    @Override \n");
        writer.write("    public void onTerminate() { \n\n");
        writer.write("        proxy.onLowMemory(); \n\n");
        writer.write("    } \n");

        writer.write("    @Override \n");
        writer.write("    public void onConfigurationChanged(Object configuration) { \n\n");
        writer.write("        proxy.onConfigurationChanged(configuration); \n\n");
        writer.write("    } \n\n");

        writer.write("    @Override \n");
        writer.write("    public int getPriority() { \n\n");
        writer.write("        return proxy.getPriority(); \n\n");
        writer.write("    } \n\n");

        writer.write("} \n\n");
    }


}
